package com.westmarine.qr_hybris.util;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.Select;


public class MethodAction {
	public static String url="http://qr-hybris.westmarine.net/";
	public enum CardType {Visa, Mastercard, AmericanExpress, Discover, JCB};
	public enum LoyaltyType {MEMBER, GOLD, SILVER, NO};
	
	public static void paymentMethodEnterNewCard(WebDriver driver, CardType cardType,
						String cardNumber, String securityCodeForm, 
						String expiryMonth, String expiryYear,
						String nameOnCard, String certificateNumber, 
						String giftCardNumber, String voucherCode){
		WebElement cardNumberWE = driver.findElement(By.id("cardNumber"));
		WebElement securityCodeFormWE = driver.findElement(By.id("securityCodeForm"));
		Select expiryMonthSel = new Select(driver.findElement(By.id("ExpiryMonth")));
		Select expiryYearSel = new Select(driver.findElement(By.id("ExpiryYear")));
		WebElement nameOnCardWE = driver.findElement(By.id("nameOnCard"));
		WebElement certificateNumberWE = driver.findElement(By.name("certificateNumber"));
		WebElement giftCardNumberWE = driver.findElement(By.id("giftCardNumber"));
		WebElement voucherCodeWE = driver.findElement(By.name("voucherCode"));
		Select cardTypeSel = new Select(driver.findElement(By.id("cardType")));
		
		cardTypeSel.selectByVisibleText(cardType.toString());	
		cardNumberWE.clear();
		cardNumberWE.sendKeys(cardNumber);
		securityCodeFormWE.clear();
		securityCodeFormWE.sendKeys(securityCodeForm);
		expiryMonthSel.selectByVisibleText(expiryMonth);
		expiryYearSel.selectByVisibleText(expiryYear);
		nameOnCardWE.clear();
		nameOnCardWE.sendKeys(nameOnCard);
		certificateNumberWE.sendKeys(certificateNumber);
		driver.findElement(By.id("giftcert-redeem")).click();
		giftCardNumberWE.sendKeys(giftCardNumber);
		driver.findElement(By.id("giftcard-redeem")).click();
		voucherCodeWE.sendKeys(voucherCode);
		driver.findElement(By.id("promo-redeem")).click();
	}
	
	public static void myAccountEnterNewCard(WebDriver driver, CardType cardType,
			String cardNumber,
			String expiryMonth, String expiryYear,
			String nameOnCard){
		WebElement cardNumberWE = driver.findElement(By.id("cardNumber"));
		Select expiryMonthSel = new Select(driver.findElement(By.id("ExpiryMonth")));
		Select expiryYearSel = new Select(driver.findElement(By.id("ExpiryYear")));
		WebElement nameOnCardWE = driver.findElement(By.id("nameOnCard"));
		Select cardTypeSel = new Select(driver.findElement(By.id("cardType")));
		
		cardTypeSel.selectByVisibleText(cardType.toString());	
		cardNumberWE.clear();
		cardNumberWE.sendKeys(cardNumber);
		expiryMonthSel.selectByVisibleText(expiryMonth);
		expiryYearSel.selectByVisibleText(expiryYear);
		nameOnCardWE.clear();
		nameOnCardWE.sendKeys(nameOnCard);
		
	}
	
	public static void paymentMethodSelectUseMyShippingAddrForBillingAddr(WebDriver driver){
		driver.findElement(By.id("differentAddress")).click();
	}
	
	public static void deliveryEnterShippingAddressClick(WebDriver driver){
		driver.findElement(By.xpath("//input[@id='__enterShippingAddress']")).click();
	}
	
	public static void deliveryShippingAddreesEnterShippingAddress(WebDriver driver, 
						String country, String firstName, String lastName,
						String line1, String line2, String city,
						String state, String zipCode, String phone ){
		Select countrySel = new Select(driver.findElement(By.id("address.country")));
		countrySel.selectByVisibleText(country);
		driver.findElement(By.id("address.firstName")).sendKeys(firstName);
		driver.findElement(By.id("address.surname")).sendKeys(lastName);
		driver.findElement(By.id("address.line1")).sendKeys(line1);
		driver.findElement(By.id("address.line2")).sendKeys(line2);
		driver.findElement(By.id("address.townCity")).sendKeys(city);
		Select stateSel = new Select(driver.findElement(By.id("address.region")));
		stateSel.selectByVisibleText(state);
		driver.findElement(By.id("address.postcode")).sendKeys(zipCode);
		driver.findElement(By.id("address.phoneNumber_del")).sendKeys(phone);
	}
	
	public static void myAccountEnterShippingAddreesForPayment(WebDriver driver, 
			String country, String firstName, String lastName,
			String line1, String line2, String city,
			String state, String zipCode, String phone ){
		Select countrySel = new Select(driver.findElement(By.id("paymentAddressCountry")));
		countrySel.selectByVisibleText(country);
		driver.findElement(By.id("address.firstName")).sendKeys(firstName);
		driver.findElement(By.id("address.surname")).sendKeys(lastName);
		driver.findElement(By.id("address.line1")).sendKeys(line1);
		driver.findElement(By.id("address.line2")).sendKeys(line2);
		driver.findElement(By.id("address.townCity")).sendKeys(city);
		Select stateSel = new Select(driver.findElement(By.id("address.region")));
		stateSel.selectByVisibleText(state);
		driver.findElement(By.id("address.postcode")).sendKeys(zipCode);
		driver.findElement(By.id("address.phoneNumber_del")).sendKeys(phone);
	}
	
	public static void deliveryShippingAddreesEnterFreightForwardAddress(WebDriver driver, 
			String freightBusinessName, String freightCustomerNumber, 
			String freightForwardCountry,String firstName, String lastName,
			String line1, String line2, String city,
			String state, String zipCode, String phone,
			String destinationCountryIso, String destinationFirstName,
			String destinationLastName, String destinationLine1,
			String destinationLine2, String destinationCity, String destinationState,
			String destinationZipCode, String destinationPhone
			){


		driver.findElement(By.id("address.freightBusinessName")).sendKeys(freightBusinessName);
		driver.findElement(By.id("address.freightCustomerNumber")).sendKeys(freightCustomerNumber);
		Select freightForwardCountrySel = new Select(driver.findElement(By.id("freightforward.country")));
		freightForwardCountrySel.selectByVisibleText(freightForwardCountry);
		driver.findElements(By.name("firstName")).get(1).sendKeys(firstName);
		driver.findElements(By.name("lastName")).get(1).sendKeys(lastName);
		driver.findElements(By.name("line1")).get(1).sendKeys(line1);
		driver.findElements(By.name("line2")).get(1).sendKeys(line2);
		driver.findElements(By.name("townCity")).get(1).sendKeys(city);
		Select stateSel = new Select(driver.findElements(By.name("regionIso")).get(1));
		stateSel.selectByVisibleText(state);
		driver.findElements(By.name("postcode")).get(1).sendKeys(zipCode);
		driver.findElements(By.name("phoneNumber")).get(1).sendKeys(phone);
		
		Select destinationCountryIsoSel = new Select(driver.findElement(By.id("freightdestination.country")));
		destinationCountryIsoSel.selectByVisibleText(destinationCountryIso);
		driver.findElement(By.name("destinationAddress.firstName")).sendKeys(destinationFirstName);
		driver.findElement(By.name("destinationAddress.lastName")).sendKeys(destinationLastName);
		driver.findElement(By.name("destinationAddress.line1")).sendKeys(destinationLine1);
		driver.findElement(By.name("destinationAddress.line2")).sendKeys(destinationLine2);
		driver.findElement(By.name("destinationAddress.townCity")).sendKeys(destinationCity);
		Select destinationStateSel = new Select(driver.findElement(By.name("destinationAddress.regionIso")));
		destinationStateSel.selectByVisibleText(destinationState);
		driver.findElement(By.name("destinationAddress.postcode")).sendKeys(destinationZipCode);
		driver.findElement(By.name("destinationAddress.phoneNumber")).sendKeys(destinationPhone);	
}
	
	
	public static void deliveryShippingAddressFreightForwardAddressClick(WebDriver driver){
		driver.findElement(By.id("__enterFreightForwardAddress")).click();
	}
	
	public static void deliveryShippingAddressFindStoreClick(WebDriver driver){
		driver.findElement(By.id("find_store")).click();
	}
	
	public static void deliveryShippingMethodContinueClick(WebDriver driver){
		driver.findElement(By.xpath("//div[@id='content']/div[2]/div[1]/div[2]/button")).click();
	}
	
	public static void deliveryShippingAddressContinueClick(WebDriver driver){
		driver.findElement(By.xpath("//div[@id='content']/div/div[2]/div[1]/div[2]/button")).click();
	}
	
	public static void deliveryShippingMethodOptionSelect(WebDriver driver, Integer option){
		//0: first option; 1: second option;
		driver.findElements(By.name("delivery_method")).get(option).click();
	}
	
	public static void paymentMethodContinueClick(WebDriver driver){
		driver.findElement(By.xpath("//div[@id='content']/div[2]/div[1]/div[2]/button")).click();
	}
	
	public static void paymentMethodSelectCardUsedForLoggedInUser(WebDriver driver, Integer option, String securityCode){
		//1: first option; 2: second option __paymentinfo1
		driver.findElement(By.id("__paymentinfo"+option.toString())).click();
		
		for (WebElement securityCodeWE : driver.findElements(By.id("SecurityCode"))){
			if (securityCodeWE.isDisplayed()){
				securityCodeWE.sendKeys(securityCode);
				break;
			}
		}
	}
			
	public static void login(WebDriver driver, String username, String password){
		driver.get(url+"login");
		//driver.navigate().to("javascript:document.getElementById(‘overridelink’).click()");
		WebElement j_username = driver.findElement(By.id("j_username"));
		j_username.sendKeys(username);
		WebElement j_password = driver.findElement(By.id("j_password"));
		j_password.sendKeys(password);
		WebElement submit = driver.findElement(By.xpath("//form[@id='loginForm']/div[2]/button"));
		submit.click();		
	}
	
	public static void loginAsReturnCustomer(WebDriver driver, String username, String password){
		WebElement j_username = driver.findElement(By.id("j_username"));
		j_username.sendKeys(username);
		WebElement j_password = driver.findElement(By.id("j_password"));
		j_password.sendKeys(password);
		WebElement submit = driver.findElement(By.xpath("//form[@id='loginForm']/div[2]/button"));
		submit.click();		
	}
	
	public static void loginAsGuest(WebDriver driver, String username){
		WebElement guestEmail = driver.findElement(By.id("guest.email"));
		guestEmail.sendKeys(username);
		WebElement submit = driver.findElement(By.xpath("//form[@id='guestForm']/div[2]/button"));
		submit.click();		
	}
	
	public static void pdpAddToCartButtonClick(WebDriver driver){
		WebElement addToCartButton = driver.findElement(By.id("addToCartButton"));
		addToCartButton.click();
	}
	
	public static void pdpEnterQuantityBachelor(WebDriver driver, Integer quantity){
		WebElement qty = driver.findElement(By.id("qty"));
		qty.clear();
		qty.sendKeys(quantity.toString());
	}
	
	public static void pdpClickCheckoutBachelor(WebDriver driver){
		WebElement chechoutCartPopup = driver.findElement(By.xpath("//div[@id='cart_popup']/div[3]/a"));
		chechoutCartPopup.click();	
	}
	
	public static void cartClickCheckout(WebDriver driver){
		WebElement checkoutButtonTop = driver.findElement(By.id("checkoutButtonTop"));
		checkoutButtonTop.click();
	}
	
	public static void cartRemoveProduct(WebDriver driver, Integer productPosition){
		//0:first; 1: second;...
		driver.findElement(By.id("RemoveProduct_"+productPosition.toString())).click();;
	}
	
	public static int cartGetProductQuantity(WebDriver driver, Integer productPosition){
		//0:first; 1: second;...
		String quantity = driver.findElement(By.id("quantity"+productPosition.toString())).getAttribute("value");
		return Integer.parseInt(quantity);
	}
	
	public static void cartProductQuantityUpdate(WebDriver driver, Integer productPosition, Integer quantity){
		//0:first; 1: second;...
		driver.findElement(By.id("quantity"+productPosition.toString())).clear();
		driver.findElement(By.id("quantity"+productPosition.toString())).sendKeys(quantity.toString());
		driver.findElement(By.id("QuantityProduct_" + productPosition.toString())).click();
	}
	
	public static void searchSku(WebDriver driver, String sku){
		 driver.get(url);
		 WebElement search = driver.findElement(By.id("search"));
		 search.sendKeys(sku);
		 WebElement searchButton = driver.findElement(By.xpath("//header[@id='header']/div[2]/div[3]/form/input[2]"));
		 searchButton.click();
	}
	
	public static void search(WebDriver driver, String word){
		 driver.get(url);
		 driver.findElement(By.id("search")).sendKeys(word);
		 driver.findElement(By.xpath("//header[@id='header']/div[2]/div[3]/form/input[2]")).click();
	}
	
	public static void searchSkuAddToCartBachelor(WebDriver driver, String sku){
		searchSku(driver, sku);
		//WebElement addToCartButton = driver.findElement(By.id("addToCartButton"));
		//addToCartButton.click();
		pdpAddToCartButtonClick(driver);
	}
	
	public static void searchSkuAddToCartBachelor(WebDriver driver, String sku, Integer quantity){
		searchSku(driver, sku);
//		WebElement qty = driver.findElement(By.id("qty"));
//		qty.clear();
//		qty.sendKeys(quantity.toString());
		pdpEnterQuantityBachelor(driver, quantity);
		pdpAddToCartButtonClick(driver);
	}
	
	public static void searchSkuAddToCartBachelorGoToCart(WebDriver driver, String sku){
		searchSkuAddToCartBachelor(driver, sku);
//		WebElement chechoutCartPopup = driver.findElement(By.xpath("//*[@id='cart_popup']/div[3]/a"));
//		chechoutCartPopup.click();	
		pdpClickCheckoutBachelor(driver);
	}
	
	public static void searchSkuAddToCartBachelorGoToCart(WebDriver driver, String sku, Integer quantity){
		searchSkuAddToCartBachelor(driver, sku, quantity);
		pdpClickCheckoutBachelor(driver);		
	}
	
	public static void searchSkuAddToCartBachelorGoToCartGoToCheckout(WebDriver driver, String sku){
		searchSkuAddToCartBachelorGoToCart(driver, sku);
		cartClickCheckout(driver);
	}
	
	public static void searchSkuAddToCartBachelorGoToCartGoToCheckout(WebDriver driver, String sku, Integer quantity){
		searchSkuAddToCartBachelorGoToCart(driver, sku, quantity);
		cartClickCheckout(driver);
	}
	
	public static void summaryPlaceOrderClick(WebDriver driver){
		driver.findElement(By.xpath("//div[@id='checkout-cart-details']/a")).click();
	}
	
	public static String getQuantityOfSku(WebDriver driver, String sku){
		searchSku(driver, sku);
		String quantity = driver.findElement(By.id("qty")).getAttribute("Value");
		System.out.println(quantity);
		return quantity;
	}
	
	public static void registerEnterNewUser(WebDriver driver, String email, String password, String checkPwd, 
			String firstName, String lastName, String country,
			String line1, String line2, String townCity,
			String region, String postcode, String phone){
		
		driver.get(url+"/register");
		driver.findElement(By.id("register.email")).sendKeys(email);
		driver.findElement(By.id("password")).sendKeys(password);
		driver.findElement(By.id("register.checkPwd")).sendKeys(checkPwd);
		driver.findElement(By.id("register.firstName")).sendKeys(firstName);
		driver.findElement(By.id("register.lastName")).sendKeys(lastName);
		Select countrySelect = new Select(driver.findElement(By.id("address.country")));
		countrySelect.selectByVisibleText(country);
		driver.findElement(By.id("address.line1")).sendKeys(line1);
		driver.findElement(By.id("address.line2")).sendKeys(line2);
		driver.findElement(By.id("address.townCity")).sendKeys(townCity);

		Select regionSelect = new Select(driver.findElement(By.id("address.region")));
		regionSelect.selectByVisibleText(region);
		driver.findElement(By.id("address.postcode")).sendKeys(postcode);
		driver.findElement(By.id("register.phone")).sendKeys(phone);
	}
	
	public static void registerEnterNewUser(WebDriver driver, String email, String password, String checkPwd, 
			String firstName, String lastName, String country,
			String line1, String line2, String townCity,
			String region, String postcode, String phone,
			boolean activeEmailOffers, boolean activePrintedOffers,
			LoyaltyType loyalty){
		
		driver.get(url+"/register");
		driver.findElement(By.id("register.email")).sendKeys(email);
		driver.findElement(By.id("password")).sendKeys(password);
		driver.findElement(By.id("register.checkPwd")).sendKeys(checkPwd);
		driver.findElement(By.id("register.firstName")).sendKeys(firstName);
		driver.findElement(By.id("register.lastName")).sendKeys(lastName);
		Select countrySelect = new Select(driver.findElement(By.id("address.country")));
		countrySelect.selectByVisibleText(country);
		driver.findElement(By.id("address.line1")).sendKeys(line1);
		driver.findElement(By.id("address.line2")).sendKeys(line2);
		driver.findElement(By.id("address.townCity")).sendKeys(townCity);

		Select regionSelect = new Select(driver.findElement(By.id("address.region")));
		regionSelect.selectByVisibleText(region);
		driver.findElement(By.id("address.postcode")).sendKeys(postcode);
		driver.findElement(By.id("register.phone")).sendKeys(phone);

		WebElement activeEmailOffersCheckBox = driver.findElement(By.name("activeEmailOffers"));
		WebElement activePrintedOffersCheckBox = driver.findElement(By.name("activePrintedOffers"));
		WebElement loyaltyMemberRadio = driver.findElement(By.id("loyalty.member"));
		WebElement loyaltyGoldRadio = driver.findElement(By.id("loyalty.gold"));
		WebElement loyaltySilverRadio = driver.findElement(By.id("loyalty.silver"));
		WebElement loyaltyNoRadio = driver.findElement(By.id("loyalty.no"));
		
		if (activeEmailOffers){
			if(!activeEmailOffersCheckBox.isSelected())
			activeEmailOffersCheckBox.click();
		}
		else{
			if(activeEmailOffersCheckBox.isSelected())
			activeEmailOffersCheckBox.click();
		}
		
		if (activePrintedOffers){
			if(!activePrintedOffersCheckBox.isSelected())
			activePrintedOffersCheckBox.click();
		}
		else{
			if(activePrintedOffersCheckBox.isSelected())
			activePrintedOffersCheckBox.click();
		}
		
		switch (loyalty){
			case MEMBER:{
				loyaltyMemberRadio.click();
				break;
			}
			case GOLD:{
				loyaltyGoldRadio.click();
				break;
			}
			case SILVER:{
				loyaltySilverRadio.click();
				break;
			}
			case NO:{
				loyaltyNoRadio.click();
				break;
			}
		}
		
	}
	
	public static void registerClickSubmitButton(WebDriver driver){
		driver.findElement(By.tagName("button")).click();

	}
	
	public static void storeLocator(WebDriver driver, String zipCode){
		driver.navigate().to("http://qr-hybris.westmarine.net");
		driver.findElement(By.linkText("Store Finder")).click();
		driver.findElement(By.id("storelocator-query")).clear();
	    driver.findElement(By.id("storelocator-query")).sendKeys(zipCode);
	    driver.findElement(By.id("storeFinderForm")).findElement(By.className("search")).click();
	}
	
	public static void myAccountPaymentDetailAddNewCardClick(WebDriver driver){
		driver.findElement(By.linkText("Add New Card")).click();
	}
	

}
